package com.chookss.tiku.adapter

import android.text.Html
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.chookss.R
import com.chookss.tiku.entity.LeftEntiry
import com.chookss.tools.Utils
import com.johnrambo.ktea.ktExtends.onclick
import kotlinx.android.synthetic.main.item_right2.view.*

class RightListAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
    private var datas: List<LeftEntiry>? = null

    var listener: ((LeftEntiry) -> Unit)? = null
    private val itemsList = mutableListOf<String>()

    override fun getItemCount() = datas?.size ?: 0
    override fun getItemViewType(position: Int) = datas!![position].lvlNo

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        if (viewType == 1) {
            return object : RecyclerView.ViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.item_right_title, parent, false)) {}
        } else {
            return object : RecyclerView.ViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.item_right2, parent, false)) {}
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val entity = datas!![position]

        if (entity.lvlNo == 1) {
            if (entity.childList.isNotEmpty()) {
                (holder.itemView as TextView).text = entity.catalogName
            } else {
                /*
                To Goodsong:这里需求略有变化，为：
                       若2级下面 没有3级的chiltList，则把2级直接展示成3级的样式（即带熟练度的3级样式）
                       产品的意思为：该控件可以同时适应2级、3级的需求

                       ps:这里我与产品有些争议，我认为如果2级下面没有3级数据，则直接不展示2级，即：(holder.itemView as TextView).text = ""；
                          如果强制把将2级数据做为3级数据，则会打乱程序的逻辑性

                          你判断一下，我说的是否正确，if(不会打乱程序的逻辑) 则改之 else 我再与产品撕逼……

                 */
                (holder.itemView as TextView).text = ""
            }

        } else {
            holder.itemView.run {
                tvTitle.text = entity.catalogName
                tvSubjectNum.text = "共"+Utils.changeNumber(entity.subjectNum)+"道"
                tvScore.text = "满分"+Utils.changeNumber(entity.score)+"分"
                tvTime.text = ""+Utils.changeNumber(entity.timeLength)+"分钟"
                val str = "练习<font color='#E12416'>"+ Utils.changeNumber(entity.allAnswerNum)+"</font>次"
                tvNum.text=Html.fromHtml(str)
                ratingBar.numStars = 5
                if (Utils.isNull(entity.avgHard)) {
                    ratingBar.rating = 0f
                } else {
                    ratingBar.rating = entity.avgHard.toFloat()
                }
                onclick {
                    for (data in datas!!) {
                        data!!.isSelected = false
                    }
                    datas!![position].isSelected = true
                    notifyDataSetChanged()

                    var entity = datas!![position]
                    listener?.let {
                        it(entity)
                    }
                }
            }
        }

    }


    fun update(list: List<LeftEntiry>) {
        datas = list
        notifyDataSetChanged()
    }


/*override fun getItemCount() {

}*/
}